package com.tanjun.userservice.web.biz.admin;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.tanjun.common.exception.BizException;
import com.tanjun.common.response.PageResponse;
import com.tanjun.common.utils.ConvertUntil;
import com.tanjun.userservice.apiservice.request.role.AdminRoleIdListRequest;
import com.tanjun.userservice.apiservice.request.role.AdminRoleMenuRequest;
import com.tanjun.userservice.apiservice.request.role.AdminRolePageRequestRequest;
import com.tanjun.userservice.apiservice.request.role.AdminRoleRequest;
import com.tanjun.userservice.apiservice.response.adminrole.admin.AdminRoleResponse;
import com.tanjun.userservice.web.service.input.AdminRoleBo;
import com.tanjun.userservice.web.service.menu.entity.BaseAdminMenuEntity;
import com.tanjun.userservice.web.service.menu.service.BaseAdminMenuService;
import com.tanjun.userservice.web.service.menu.service.BaseAdminRoleMenuService;
import com.tanjun.userservice.web.service.role.entity.BaseAdminRoleEntity;
import com.tanjun.userservice.web.service.role.BaseAdminRoleService;
import com.tanjun.web.service.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class AdminRoleBiz {
    @Autowired
    private BaseAdminRoleService baseAdminRoleService;
    @Autowired
    private BaseAdminRoleMenuService baseAdminRoleMenuService;
    @Autowired
    private BaseAdminMenuService baseAdminMenuService;
    /**
     * 新增角色
     *
     * @param paramVo 新增参数
     */
    
    public void addRole(AdminRoleRequest paramVo) {
        Assert.isNull(paramVo.getRoleId());
        AdminRoleBo adminRoleBo= ConvertUntil.convert(paramVo, AdminRoleBo.class);
        adminRoleBo.setOperator(LoginUserUtil.getAdminOperate());
        baseAdminRoleService.saveOrUpdateRole(adminRoleBo);
    }

    /**
     * 修改角色
     *
     * @param paramVo 修改参数
     */
    
    public void updateRole(AdminRoleRequest paramVo) {
        AdminRoleBo adminRoleBo= ConvertUntil.convert(paramVo, AdminRoleBo.class);
        adminRoleBo.setOperator(LoginUserUtil.getAdminOperate());
        baseAdminRoleService.saveOrUpdateRole(adminRoleBo);
    }

    /**
     * 删除角色
     *
     * @param paramVo 角色id列表
     */
    
    @Transactional(rollbackFor = Exception.class)
    public void delRoles(AdminRoleIdListRequest paramVo) {
        paramVo.getRoleIds().forEach(baseAdminRoleService::delRole);
    }

    /**
     * 根据id信息获取角色基本信息
     *
     * @param id 角色id
     * @return 角色基本信息
     */
    
    public AdminRoleResponse getRoleById(Long id) {
       BaseAdminRoleEntity role= baseAdminRoleService.getRoleById(id);
       if(role!=null){
           return ConvertUntil.convert(role, AdminRoleResponse.class);
       }
        return null;
    }

    /**
     * 分页查询角色信息
     *
     * @param paramVo 查询参数
     * @return 分页数据
     */
    
    public PageResponse<AdminRoleResponse> listPageRole(AdminRolePageRequestRequest paramVo) {
//       Page<BaseAdminRole> page= baseAdminRoleService.page(new Page<>(paramVo.getPageNum(), paramVo.getPageSize()),
//               Wrappers.lambdaQuery(BaseAdminRole.class)
//                       .like(StrUtil.isNotEmpty(paramVo.getRoleCode()),BaseAdminRole::getRoleCode,paramVo.getRoleCode())
//                       .like(StrUtil.isNotEmpty(paramVo.getRoleName()),BaseAdminRole::getRoleName,paramVo.getRoleName())
//                       .between(paramVo.getCreateTimeRange()!=null&&paramVo.getCreateTimeRange().length==2,
//                               BaseAdminRole::getCreateTime,paramVo.getCreateTimeRange()!=null?paramVo.getCreateTimeRange()[0]:null,
//                               paramVo.getCreateTimeRange()!=null?paramVo.getCreateTimeRange()[1]:null
//                               )
//                       .orderByDesc(BaseAdminRole::getRoleId)
//       );
//        return ConvertUntil.convert(page, AdminRoleDto.class);
        return null;
    }

    /**
     * 获取所有的角色信息
     *
     * @return 角色信息
     */
    
    public List<AdminRoleResponse> listAll() {
//        List<BaseAdminRole> list=baseAdminRoleService.list(Wrappers.lambdaQuery(BaseAdminRole.class)
//                .orderByAsc(BaseAdminRole::getRoleId)
//        );
//        return ConvertUntil.convert(list,AdminRoleDto.class);
        return null;
    }

    /**
     * 保存角色对应的权限
     *
     * @param paramVo 角色的权限
     */
    
    public void saveRoleMenu(AdminRoleMenuRequest paramVo) {
        BaseAdminRoleEntity role=baseAdminRoleService.getRoleById(paramVo.getRoleId());
        if(role==null){
            throw new BizException("角色信息不存在");
        }
        if(CollectionUtil.isNotEmpty(paramVo.getMenuIdList())) {
            List<BaseAdminMenuEntity> listMenu = baseAdminMenuService.listByIds(paramVo.getMenuIdList());
            if (listMenu.size() != paramVo.getMenuIdList().size()) {
                throw new BizException("菜单信息有误");
            }
        }
        baseAdminRoleMenuService.saveRoleMenus(paramVo.getRoleId(), paramVo.getMenuIdList());

    }

    /**
     * 获取角色所有的菜单
     *
     * @param roleId 角色id
     * @return 返回对应的菜单id list
     */
    
    public List<Long> getRoleMenuIdList(Long roleId) {
        return baseAdminRoleMenuService.getRoleMenuIdList(roleId);
    }

    /**
     * 根据用户id信息获取用户所有的菜单
     *
     * @param userId 用户id
     * @return 对应的菜单信息
     */
    
    public List<AdminRoleResponse> getRolesMenuList(Long userId) {
        return Collections.emptyList();
    }
}
